﻿Imports Microforma.BEL
Imports MICROFORMA.BLL

Public Class frmTypeDocAndIndex
    Dim indicesBL As IndicesBL
    Dim tiposDocBl As TypeDocBL
    Sub New()

        indicesBL = New IndicesBL
        tiposDocBl = New TypeDocBL
        InitializeComponent()

    End Sub

    Private Sub lnkNewIndice_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkNewIndice.LinkClicked

        If lsttiposDoc.SelectedItems.Count < 1 Then
            Exit Sub
        End If

        Dim _frmIndexAdd As New frmIndexAdd
        _frmIndexAdd.IdTipoDocumental = lsttiposDoc.SelectedItems(0).Text
        _frmIndexAdd.NombreTipoDocumental = lsttiposDoc.SelectedItems(0).SubItems(1).Text

        _frmIndexAdd.ShowDialog()
        'Call LoadListaIndices()
        _frmIndexAdd.Dispose()
    End Sub

    Private Sub frmIndex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Call LoadList()
        Call LoadTipoDocumentales()

    End Sub
    Private Sub LoadTipoDocumentales()
        lsttiposDoc.Items.Clear()

        Dim _DataTableTiposDoc As DataTable = tiposDocBl.GetListAll

        Dim lviItem As ListViewItem
        For Each dtrFila As DataRow In _DataTableTiposDoc.Rows
            lviItem = New ListViewItem
            lviItem.Text = dtrFila("IdPlantilla")
            lviItem.SubItems.Add(dtrFila("Nombre"))
            lviItem.SubItems.Add(dtrFila("Descripcion"))
            lviItem.SubItems.Add(dtrFila("FechaCreacion"))
            lviItem.SubItems.Add(dtrFila("Habilitado"))

            lviItem.ImageIndex = 1
            lsttiposDoc.Items.Add(lviItem)
        Next


    End Sub
    Private Sub LoadListaIndices(ByVal IntIdTypeDoc As Integer)
        lstvIndices.Items.Clear()

        Dim _datableIndices As DataTable = indicesBL.List(IntIdTypeDoc)
        Dim lviItem As ListViewItem

        For Each dtrFila As DataRow In _datableIndices.Rows
            lviItem = New ListViewItem
            lviItem.Text = dtrFila("IdIndice")
            lviItem.SubItems.Add(dtrFila("Nombre"))
            lviItem.SubItems.Add(dtrFila("TipoDatoDes"))
            lviItem.ImageIndex = 1
            lstvIndices.Items.Add(lviItem)
        Next

    End Sub

    Private Sub lnkUpdateIndice_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkUpdateIndice.LinkClicked
        If lstvIndices.SelectedItems.Count > 0 Then
            Call ShowFormListAdd(True, CShort(lstvIndices.SelectedItems(0).Text.Trim))
        End If
    End Sub

    Private Sub ShowFormListAdd(Optional ByVal bolUpdate As Boolean = Nothing, Optional ByVal idIndice As Short = 0)

        Dim frmIndexAdd As New frmIndexAdd
        frmIndexAdd.bolUpdate = bolUpdate
        frmIndexAdd.idIndice = idIndice
        frmIndexAdd.ShowDialog()
        frmIndexAdd.Dispose()

    End Sub


    Private Sub lstvIndices_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstvIndices.DoubleClick
        If lstvIndices.SelectedItems.Count > 0 Then
            Call ShowFormListAdd(True, CShort(lstvIndices.SelectedItems(0).Text.Trim))
        End If

    End Sub

    

    Private Sub LinkLabel6_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkEliminarIndice.LinkClicked
        If MsgBox("Esta seguro de eliminar el indice [" & lstvIndices.SelectedItems(0).SubItems(1).Text & "]", MsgBoxStyle.YesNo + MsgBoxStyle.Question, Application.ProductName) = MsgBoxResult.Yes Then
            indicesBL.Eliminar(CInt(lstvIndices.SelectedItems(0).Text))

        End If
    End Sub

    Private Sub LnkAdd_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkAdd.LinkClicked
        Dim _frmAddTypeDoc As New frmTypeDocAdd
        _frmAddTypeDoc.ShowDialog()
        Call LoadTipoDocumentales()
    End Sub

    Private Sub lsttiposDoc_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lsttiposDoc.DoubleClick
        Call ModificarTypeDoc()
    End Sub

    Private Sub LnkModificarTypDoc_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LnkModificarTypDoc.LinkClicked
        Call ModificarTypeDoc()
    End Sub
    Sub ModificarTypeDoc()
        If lsttiposDoc.SelectedItems.Count > 0 Then
            Dim _frmAddTypeDoc As New frmTypeDocAdd
            _frmAddTypeDoc.IdTipoDoc = CShort(lsttiposDoc.SelectedItems(0).Text.Trim)
            _frmAddTypeDoc.ShowDialog()
            Call LoadTipoDocumentales()
        End If
    End Sub

    Private Sub lsttiposDoc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsttiposDoc.SelectedIndexChanged
        If lsttiposDoc.SelectedItems.Count = 0 Then Exit Sub
        Call LoadListaIndices(lsttiposDoc.SelectedItems(0).Text)
    End Sub
End Class